# -*- coding: utf-8 -*-
import sys
sys.path.append('../..')
from common.config import *
from db.mysql import MysqlClient
from itertools import product
import requests, json
from common.helper import Helper
from pprint import pprint
from threading import Thread

class Spider(object):
    def __init__(self):
        self.mysql = MysqlClient()
        self.name = "中意一生保B终身保障计划"

    # 生成产品数据
    def genProductInfo(self):

        professions = {
            "国家机关、党群组织、企业、事业单位人员": [{
                "text": "国家机关、党群组织、企业、事业单位人员",
                "value": "A01",
                "type": "1"
            }
            ],
            "卫生专业技术人员": [{
                "text": "卫生专业技术人员",
                "value": "M02",
                "type": "1"
            }],
            "金融业务人员": [{
                "text": "金融业务人员",
                "value": "S02",
                "type": "1"
            }],
            "法律专业人员": [{
                "text": "法律专业人员",
                "value": "S06",
                "type": "1"
            }],
            "教学人员": [{
                "text": "教学人员",
                "value": "O03",
                "type": "1"
            }],
            "新闻出版及文学艺术工作人员": [{
                "text": "新闻出版及文学艺术工作人员",
                "value": "L02",
                "type": "1"
            }],
            "宗教职业者": [{
                "text": "宗教职业者",
                "value": "P02",
                "type": "1"
            }],
            "邮政和电信业务人员": [{
                "text": "邮政和电信业务人员",
                "value": "Q01",
                "type": "1"
            }],
            "商业、服务业人员": [{
                "text": "商业、服务业人员",
                "value": "R04",
                "type": "1"
            }],
            "农、林、牧、渔、水利业生产人员": [{
                "text": "农、林、牧、渔、水利业生产人员",
                "value": "B02",
                "type": "1"
            }],
            "运输人员": [{
                "text": "运输人员",
                "value": "F114",
                "type": "1"
            }],
            "地质勘测人员": [{
                "text": "地质勘测人员",
                "value": "I50",
                "type": "1"
            }],
            "建筑工程技术人员": [{
                "text": "建筑工程技术人员",
                "value": "I06",
                "type": "1"
            }],
            "加工制造、检验及计量人员": [{
                "text": "加工制造、检验及计量人员",
                "value": "K22",
                "type": "1"
            }],
            "行政及文职现役军人": [{
                "text": "行政及文职现役军人",
                "value": "V01",
                "type": "1"
            }],
            "退休人员": [{
                "text": "退休人员",
                "value": "T03",
                "type": "1"
            }],
            "家庭主妇": [{
                "text": "家庭主妇",
                "value": "T01",
                "type": "1"
            }],
            "学生": [{
                "text": "学生",
                "value": "O04",
                "type": "1"
            }],
            "儿童": [{
                "text": "儿童",
                "value": "T04",
                "type": "1"
            }],
            "企业班车司机": [{
                "text": "企业班车司机",
                "value": "F113",
                "type": "1"
            }],
            "造船业工程师": [{
                "text": "造船业工程师",
                "value": "I36",
                "type": "1"
            }],
            "木制家具制造工人": [{
                "text": "木制家具制造工人",
                "value": "K19",
                "type": "1"
            }],
            "竹木制手工艺品加工工人": [{
                "text": "竹木制手工艺品加工工人",
                "value": "K22",
                "type": "1"
            }],
            "空运业内勤人员": [{
                "text": "空运业内勤人员",
                "value": "G15",
                "type": "1"
            }],
            "餐旅业内勤人员": [{
                "text": "餐旅业内勤人员",
                "value": "H03",
                "type": "1"
            }],
            "娱乐业演员、导演": [{
                "text": "娱乐业演员、导演",
                "value": "N03",
                "type": "1"
            }],
            "家政服务人员": [{
                "text": "家政服务人员",
                "value": "T02",
                "type": "1"
            }],
            "其他高风险职业": [
                {
                    "text": "刑事或治安警察、武装警察",
                    "value": "U05",
                    "type": "D"
                },
                {
                    "text": "军校学生及入伍受训新兵",
                    "value": "V09",
                    "type": "D"
                },
                {
                    "text": "军事单位武器、弹药研究及管理人员",
                    "value": "V06",
                    "type": "D"
                },
                {
                    "text": "客货轮船长",
                    "value": "G01",
                    "type": "D"
                },
                {
                    "text": "码头工人及领班(上船作业)",
                    "value": "G05",
                    "type": "D"
                },
                {
                    "text": "缉私人员",
                    "value": "G12",
                    "type": "D"
                },
                {
                    "text": "货车司机及随车工人(4吨以上)",
                    "value": "F09",
                    "type": "D"
                },
                {
                    "text": "工程卡车司机、随车工人",
                    "value": "F14",
                    "type": "D"
                },
                {
                    "text": "民航飞机驾驶员及乘务员",
                    "value": "G33",
                    "type": "D"
                },
                {
                    "text": "直升机飞行员",
                    "value": "G34",
                    "type": "D"
                },
                {
                    "text": "建筑工程车辆驾驶员",
                    "value": "I20",
                    "type": "D"
                },
                {
                    "text": "搬运工人、建筑工人、拆迁房屋工人",
                    "value": "I40",
                    "type": "D"
                },
                {
                    "text": "隧道、地下铁工程、潜水、爆破人员",
                    "value": "I57",
                    "type": "D"
                },
                {
                    "text": "钢铁业铁工厂、机械厂工人",
                    "value": "J06",
                    "type": "D"
                },
                {
                    "text": "高压电工作人员",
                    "value": "J31",
                    "type": "D"
                },
                {
                    "text": "接触尘埃或有毒化合物之工人",
                    "value": "K15",
                    "type": "D"
                },
                {
                    "text": "邮政电信业快递员",
                    "value": "Q38",
                    "type": "D"
                },
                {
                    "text": "新闻业杂志业战地记者",
                    "value": "L04",
                    "type": "D"
                },
                {
                    "text": "户外广告招牌绘制或架设人员",
                    "value": "L12",
                    "type": "D"
                },
                {
                    "text": "武打演员、杂技演员",
                    "value": "N04",
                    "type": "D"
                },
                {
                    "text": "吧女、酒女、舞女、咖啡女郎",
                    "value": "N59",
                    "type": "D"
                },
                {
                    "text": "木材采运领班、监工",
                    "value": "D04",
                    "type": "D"
                },
                {
                    "text": "森林防火人员",
                    "value": "D16",
                    "type": "D"
                },
                {
                    "text": "驯兽人员、动物饲养人员",
                    "value": "B091",
                    "type": "D"
                },
                {
                    "text": "海洋、近海渔船工作人员",
                    "value": "C09",
                    "type": "D"
                },
                {
                    "text": "石油、天然气开采人员",
                    "value": "E18",
                    "type": "D"
                },
                {
                    "text": "勘探及矿物开采人员",
                    "value": "E19",
                    "type": "D"
                },
                {
                    "text": "火药爆竹制造及处理人员",
                    "value": "J49",
                    "type": "D"
                },
                {
                    "text": "竞技职业运动员",
                    "value": "Y65",
                    "type": "D"
                },
                {
                    "text": "高楼外部清洁工、烟囱清洁工",
                    "value": "S27",
                    "type": "D"
                },
                {
                    "text": "保安人员",
                    "value": "S041",
                    "type": "D"
                },
                {
                    "text": "极限运动、探险和考察人员",
                    "value": "Q49",
                    "type": "D"
                },
                {
                    "text": "电讯及电力设施、工程施工人员",
                    "value": "Q24",
                    "type": "D"
                }
            ]
        }

        profs = []
        for prof in professions:
            for v in professions[prof]:
                profs.append({"name": v["text"], "value": v["value"]})

        plan = {
            "planA": {
                # 年龄段
                "year": [str(i) for i in range(0, 17)],
                # 性别
                "gender": ["M", "F"],
                # 保障期限
                "expire": ["终身"],
                # 交费类型
                "payType": ["年交"],
                # 职业名称
                "profession": [""],
                # 投保对象
                "payObject": ["孩子"]

            },
            "planB": {
                # 年龄段
                "year": [str(i) for i in range(18, 51)],
                # 性别
                "gender": ["M", "F"],
                # 保障期限
                "expire": ["终身"],
                # 交费类型
                "payType": ["年交"],
                # 职业名称
                "profession": profs,
                # 投保对象
                "payObject": ["本人"]

            }
        }

        for k in plan:
            for v in product(plan[k]["year"], plan[k]["gender"], plan[k]["expire"], plan[k]["payType"],
                             plan[k]["profession"],
                             plan[k]["payObject"]):
                if v[4] != '':
                    data = {}
                    data["year"] = v[0]
                    data["gender"] = v[1]
                    data["expire"] = v[2]
                    data["payType"] = v[3]
                    data["profession"] = v[4]["name"]
                    data["professionCode"] = v[4]["value"]
                    data["payObject"] = v[5]
                else:
                    data = Helper.formatProductInfo(plan[k], v)
                    data["profession"] = ''
                    data['professionCode'] = ''
                self.mysql.cursor.execute(domain[self.name]["insert_sql"].format(**data))
                self.mysql._conn.commit()
                print(domain[self.name]["insert_sql"].format(**data))

    # 执行爬虫
    def requestJson(self, dt=None,mysql=None):
        original_sql = 'INSERT  INTO `中意一生保B终身保障计划`(`year`,`gender`,`expire`,`payType`,`interval`,`money`,`profession`,`professionCode`,`ext_item`,`payObject`,`price`)VALUES("{year}","{gender}","{expire}","{payType}","{interval}","{money}","{profession}","{professionCode}","{ext_item}","{payObject}","{price}")'
        if dt["payObject"] == "本人":
            data = {"age": dt["year"], "gender": dt["gender"], "occupationCode": dt["professionCode"], "memberId": "null"}
            types = "myself"
        else:
            data = {"age":dt["year"],"gender":dt["gender"],"occupationCode":"null","memberId":"null"}
            types = "children"

        payload = json.dumps(data)
        r = requests.post(url=domain[self.name]["url"], headers=domain[self.name]["header"], data=payload)
        if r.status_code == 200:
            data = r.json()
            years = ["fiveYears", "tenYears", "fifteenYears", "twentyYears"]
            for year in years:
                for v in self.filterResult(data, year,types):
                    if year == "fiveYears":
                        if "extPrice" not in v:
                            result = {
                                "interval": "5",
                                "money": v["money"],
                                "normalPrice": v["normalPrice"]
                            }

                            # print( result )


                        else:
                            result = {
                                "interval": "5",
                                "money": v["money"],
                                "normalPrice": v["normalPrice"],
                                "extPrice": v["extPrice"]
                            }
                            # print( result )

                    elif year == "tenYears":
                        if "extPrice" not in v:
                            result = {
                                "interval": "10",
                                "money": v["money"],
                                "normalPrice": v["normalPrice"]
                            }

                        else:
                            result = {
                                "interval": "10",
                                "money": v["money"],
                                "extPrice": v["extPrice"],
                                "normalPrice": v["normalPrice"]
                            }
                            # print( result )
                    elif year == "fifteenYears":
                        if "extPrice" not in v:
                            result = {
                                "interval": "15",
                                "money": v["money"],
                                "normalPrice": v["normalPrice"]
                            }
                            # print( result )

                        else:
                            result = {
                                "interval": "15",
                                "money": v["money"],
                                "extPrice": v["extPrice"],
                                "normalPrice": v["normalPrice"]
                            }
                            # print( result )

                    else:

                        if "extPrice" not in v:
                            result = {
                                "interval": "20",
                                "money": v["money"],
                                "normalPrice": v["normalPrice"]
                            }
                            # print( result )

                        else:
                            result = {
                                "interval": "20",
                                "money": v["money"],
                                "extPrice": v["extPrice"],
                                "normalPrice": v["normalPrice"]
                            }
                            # print( result )
                    if "extPrice"  not in result:
                        dt["interval"] = result["interval"] if "interval" in result  else ""
                        dt["money"] = result["money"] if "money" in result else ""
                        dt["ext_item"] = "0"
                        dt["price"] = result["normalPrice"] if "normalPrice" in result else ""

                        mysql.cursor.execute( original_sql.format( **dt ))
                        mysql._conn.commit()
                        print( original_sql.format( **dt) )
                    else:
                        dt["interval"] = result["interval"] if "interval" in result  else ""
                        dt["money"] = result["money"] if "money" in result else ""
                        dt["ext_item"] = result["money"]
                        dt["price"] = result["extPrice"] if "extPrice" in result else ""

                        mysql.cursor.execute(original_sql.format(**dt))
                        mysql._conn.commit()
                        print(original_sql.format(**dt))


                        dt["interval"] = result["interval"] if "interval" in result  else ""
                        dt["money"] = result["money"] if "money" in result else ""
                        dt["ext_item"] = "0"
                        dt["price"] = result["normalPrice"] if "normalPrice" in result else ""

                        mysql.cursor.execute(original_sql.format(**dt))
                        mysql._conn.commit()
                        print("写入MySQl数据库成功:\n")
                        pprint( dt )
                        #
            #更新任务结果
            data = {
                "name":self.name + "-TASK",
                "id":dt["id"],
                "state":2
            }
            mysql.updateTask( data )
            print("\n保险任务更新成功:%d/" % dt["id"])
        else:
            data = {
                "name": self.name + "-TASK",
                "id": dt["id"],
                "state": 3
            }
            mysql.updateTask(data)
            print("\n保险任务更新失败:%d/"%dt["id"])


    # 过滤数据
    def filterResult(self, data=None, interval=None,types=None):
        ext_items = data["ciQuoteTo"][interval]
        if types == "myself":
            if ext_items:
                for x in (data["lifeQuoteTo"][interval]["coveragePremiums"]):
                    if x["benefitCoverAmount"] == 100000:
                        result = {
                            "interval": interval,
                            "money": "100000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"]),
                            "extPrice": Helper.decimalParse(
                                x["benefitAnnualPremium"] + ext_items["coveragePremiums"][0]["benefitAnnualPremium"])
                        }

                    elif x["benefitCoverAmount"] == 200000:
                        result = {
                            "interval": interval,
                            "money": "200000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"]),
                            "extPrice": Helper.decimalParse(
                                x["benefitAnnualPremium"] + ext_items["coveragePremiums"][1]["benefitAnnualPremium"])
                        }

                    elif x["benefitCoverAmount"] == 300000:
                        result = {
                            "interval": interval,
                            "money": "300000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"]),
                            "extPrice": Helper.decimalParse(
                                x["benefitAnnualPremium"] + ext_items["coveragePremiums"][2]["benefitAnnualPremium"])
                        }
                    elif x["benefitCoverAmount"] == 400000:
                        result = {
                            "interval": interval,
                            "money": "400000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"]),
                            "extPrice": Helper.decimalParse(
                                x["benefitAnnualPremium"] + ext_items["coveragePremiums"][3]["benefitAnnualPremium"])
                        }
                    elif x["benefitCoverAmount"] == 500000:
                        result = {
                            "interval": interval,
                            "money": "500000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"]),
                            "extPrice": Helper.decimalParse(
                                x["benefitAnnualPremium"] + ext_items["coveragePremiums"][4]["benefitAnnualPremium"])
                        }

                    else:
                        continue

                    yield result
            else:
                for x in (data["lifeQuoteTo"][interval]["coveragePremiums"]):
                    if x["benefitCoverAmount"] == 100000:
                        result = {
                            "interval": interval,
                            "money": "100000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"])
                        }


                    elif x["benefitCoverAmount"] == 200000:
                        result = {
                            "interval": interval,
                            "money": "200000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"])
                        }

                    elif x["benefitCoverAmount"] == 300000:
                        result = {
                            "interval": interval,
                            "money": "300000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"])
                        }
                    elif x["benefitCoverAmount"] == 400000:
                        result = {
                            "interval": interval,
                            "money": "400000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"])
                        }
                    elif x["benefitCoverAmount"] == 500000:
                        result = {
                            "interval": interval,
                            "money": "500000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"])
                        }

                    else:
                        continue

                    yield result
        else:

            if ext_items:
                for x in (data["lifeQuoteTo"][interval]["coveragePremiums"]):
                    if x["benefitCoverAmount"] == 100000:
                        result = {
                            "interval": interval,
                            "money": "100000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"]),
                            "extPrice": Helper.decimalParse(
                                x["benefitAnnualPremium"] + ext_items["coveragePremiums"][0]["benefitAnnualPremium"])
                        }

                    elif x["benefitCoverAmount"] == 200000:
                        result = {
                            "interval": interval,
                            "money": "200000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"]),
                            "extPrice": Helper.decimalParse(
                                x["benefitAnnualPremium"] + ext_items["coveragePremiums"][1]["benefitAnnualPremium"])
                        }

                    else:
                        continue

                    yield result
            else:
                for x in (data["lifeQuoteTo"][interval]["coveragePremiums"]):
                    if x["benefitCoverAmount"] == 100000:
                        result = {
                            "interval": interval,
                            "money": "100000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"])
                        }


                    elif x["benefitCoverAmount"] == 200000:
                        result = {
                            "interval": interval,
                            "money": "200000",
                            "normalPrice": Helper.decimalParse(x["benefitAnnualPremium"])
                        }

                    else:
                        continue

                    yield result


    def main(self,mysql):
        while True:
            # 随机领取一条数据
            dt = mysql.randTask(self.name + "-TASK")
            if not dt:
                print("产品价格抓取完成~~~")
                return
                # 任务执行状态
            data = {
                "name": self.name + "-TASK",
                "id": dt["id"],
                "state": 1
            }
            mysql.updateTask(data)
            self.requestJson(dt,mysql)
            # 多线程

    def startProcess(self):
        for i in range(5):
            mysql = MysqlClient()
            t = Thread(target=self.main, args=(mysql,))

            t.start()


if __name__ == '__main__':
    s = Spider()
    s.startProcess()
